package com.etheart.lyric.htmlparser.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.springframework.stereotype.Service;

import com.etheart.framework.core.util.RegexUtils;
import com.etheart.model.entity.Singer;

@Service("htmlparser_SingerParseService")
public class SingerParseService {
	private static final Log logger = LogFactory
			.getLog(SingerParseService.class);

	public Singer getSingerFromParser(Parser parser,String fileName, String fileEncode, String charset)
			throws IOException {
		String idStr = RegexUtils.getMatcheGroup(".*singer_([0-9]*)\\.html",
				fileName);
		Singer singer = new Singer();
		singer.setNetId(idStr);
		return getSinger(parser, singer);
	}


	
	public Singer getSinger(Parser parser, Singer singer) {
		// 得到歌手名称
		try {
			String singerName = parseSingerName(parser);
			singer.setSingername(singerName);
		} catch (ParserException e) {
			logger.error("qqsingerid:" + singer.getNetId()
					+ " singername:解析异常", e);
			e.printStackTrace();
		}
		//歌手图片
		try {
			String imgPath = parseSingerImg(parser);
			singer.setPicture(imgPath);
		} catch (ParserException e) {
			logger.error("qqsingerid:" + singer.getNetId() + " picUrl:解析异常",
					e);
			e.printStackTrace();
		}
		// 歌手简介
		try {
			String intr = parseSingerIntro(parser);
			singer.setSingerintro(intr);
		} catch (ParserException e) {
			logger.error("qqsingerid:" + singer.getNetId()
					+ " singerintro:解析异常", e);
			e.printStackTrace();
		}
		
		
		
		// 歌手排行  靠 没有
	//	int rank = parseSingerRank(parser);

		return singer;
	}





	private String parseSingerIntro(Parser parser) throws ParserException {
		parser.reset();
		NodeFilter filter = new AndFilter(new TagNameFilter("p"),
				new HasAttributeFilter("class", "about"));
		NodeList nodeList = parser.parse(filter);
		if ( nodeList.size()<=0) {
			return null;
		}
		Tag tag = (Tag) nodeList.elementAt(0);
		return tag.toPlainTextString().replace("歌手简介：", "");

	}

	private String parseSingerImg(Parser parser) throws ParserException {
		parser.reset();
		NodeFilter imgFilter = new TagNameFilter("img");
		// 得到歌手图片地址
		NodeList nodeList = parser.parse(imgFilter);
		for (int i = 0; i < nodeList.size(); i++) {
			Tag tag = (Tag) nodeList.elementAt(i);
			String src = tag.getAttribute("src");
			if (src.indexOf("photo/singer/") != -1) {
				return src.replace("http://imgcache.qq.com/music/", "");
			}
		}
		return null;
	}

	// 解析歌手名
	private String parseSingerName(Parser parser) throws ParserException {
		parser.reset();
		NodeFilter filter = new TagNameFilter("h2");
		NodeList nodeList = parser.parse(filter);
		for (int i = 0; i < nodeList.size();) {
			Node h2 = nodeList.elementAt(i);
			Tag h2Tag = (Tag) h2;
			return h2Tag.getAttribute("title");
		}
		return null;
	}
	
	public List<String> getAlbumUrls(Parser parser) throws ParserException{
		List<String> urls = new ArrayList<String>(10);
		parser.reset();
		NodeFilter filter = new AndFilter(new TagNameFilter("li"),new HasAttributeFilter("id"));
		NodeList nodeList = parser.parse(filter);
		for (int i = 0; i < nodeList.size();i++) {
			Node node = nodeList.elementAt(i).getFirstChild();
			if (node == null) {
				continue;
			}
			Tag tag = (Tag) node ;
			urls.add(getAlbumUrlFromA(tag.toHtml()));
		}
		return urls;
	}
	
	private String getAlbumUrlFromA(String a) {
        return RegexUtils.getMatcheGroup(".*href=\"../../([^\"]*)\".*", a);
	}
}
